package com.cjh.controller;

import com.cjh.annotation.WebLog;
import com.cjh.feign.AccountFeign;
import com.cjh.pojo.Admin;
import com.cjh.pojo.User;
import com.cjh.vo.ResultMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.Objects;

@RestController
@RequestMapping("/accountApi")
public class AccountController {

    @Autowired
    private AccountFeign accountFeign;

    private final Logger logger = LoggerFactory.getLogger(AccountController.class);

    @PostMapping("/login")
    @WebLog(description = "请求=>系统登录接口")
    public ResultMap login(@RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("status") String status,
                           HttpSession session){
        if(!Objects.equals(status,"user")&&!Objects.equals(status,"admin")){
            return new ResultMap<String>(405,"根据用户名、密码、身份登录","身份不合法",0);
        }
//        //根据debug得知：loginObj从account生产者返回过来的数据类型变成LinkedHashMap，所以将他转为LinkedHashMap
//        LinkedHashMap<String,Object> loginObj = (LinkedHashMap<String, Object>) accountFeign.login(username, password, status);
//        //登录对象是否存在
//        if(!ObjectUtils.isEmpty(loginObj)){
//            switch (status){
//                //根据身份将loginObj转为对应的实体类
//                case "user":
//                    User user=new User();
//                    user.setId((Integer) loginObj.get("id"));
//                    user.setNickname(loginObj.get("nickname")+"");
//                    session.setAttribute("user",user);
//                    logger.info("session:"+session.getAttribute("user"));
//
//                    break;
//                case "admin":
//                    Admin admin=new Admin();
//                    admin.setId((Integer) loginObj.get("id"));
//                    admin.setUsername(loginObj.get("username")+"");
//                    session.setAttribute("admin",admin);
//                    logger.info("session:"+session.getAttribute("admin"));
//
//                    break;
//            }
//            return new ResultMap<String>(200,"根据用户名、密码、身份登录",status,1);
//        }
//        return new ResultMap<String>(500,"根据用户名、密码、身份登录","登陆失败，用户名或密码错误",0);

        // 获取当前用户
        Subject subject = SecurityUtils.getSubject();
        // 封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, status);
        System.out.println("login方法执行前===");
        try {
            // 执行登录方法，没有异常则成功
            subject.login(token);
            return new ResultMap<String>(200,"根据用户名、密码、身份登录",status,1);
        } catch (UnknownAccountException e) {// 用户名不存在
            return new ResultMap<String>(404,"根据用户名、密码、身份登录","用户不存在",1);
        } catch (IncorrectCredentialsException e) {// 密码错误
            return new ResultMap<String>(500,"根据用户名、密码、身份登录","密码错了",0);
        }


    }

    @GetMapping("/getLoginObj/{status}")
    @WebLog(description = "请求=>获取登录用户的信息接口")
    public ResultMap getLoginObj(@PathVariable("status") String status,HttpSession session){
        switch (status){
            case "user":
                User user = (User) session.getAttribute("user");
                return new ResultMap<Object>(200,"获取用户账号的信息",user,1);
            default:
                Admin admin = (Admin) session.getAttribute("admin");
                return new ResultMap<Object>(200,"获取管理员账号的信息",admin,1);
        }

    }

    @GetMapping("/logout")
    @WebLog(description = "请求=>系统注销接口")
    public void logout(HttpSession session) throws InterruptedException {
        Thread.sleep(1500);
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }
}
